//
//  ViewController.m
//  加速计_陀螺仪_磁力计
//
//  Created by dllo on 15/11/13.
//  Copyright © 2015年 Chris. All rights reserved.
//

#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>
@interface ViewController ()
@property(nonatomic, strong)UILabel *CMAlabel; /**< 加速度 */
@property(nonatomic, strong)UILabel *CMGlabel; /**< 陀螺仪 */
@property(nonatomic, strong)UILabel *CMMlabel; /**< 磁力计 */
@property(nonatomic, strong)CMMotionManager *motionManager;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self typeOne];
    
    [self typeTwo];
    
}

- (void)typeOne {
    self.CMAlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 50, 375, 150)];
    self.CMAlabel.numberOfLines = 5;
    self.CMAlabel.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.CMAlabel];
    //创建CMMotionManager
    self.motionManager = [CMMotionManager new];
    //创建子线程来更新数据
    NSOperationQueue *queue = [NSOperationQueue new];
    
    //如果accelerometer是可用的
    if (self.motionManager.accelerometerAvailable) {
        //设置CMMotionManager的加速度读更新频率;
        self.motionManager.accelerometerUpdateInterval = 0.1;
        //使用代码块开始获取加速度的数据
        [self.motionManager startAccelerometerUpdatesToQueue:queue withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
            NSString *labeltext;
            if (error) {
                //停止获取加速度数据
                [self.motionManager stopAccelerometerUpdates];
                labeltext = [NSString stringWithFormat:@"获取加速度数据错误 %@",error];
            }else{
                //分别获取X,Y,Z轴上的加速度数据
                labeltext = [NSString stringWithFormat:@"加速度为\n------------------\nX轴%+.2f \nY轴%+.2f \nZ轴%+.2f",
                             accelerometerData.acceleration.x,
                             accelerometerData.acceleration.y,
                             accelerometerData.acceleration.z];
            }
            //在主线程更新文本（基于默认模式调用主线程中接收器的方法）
            [self.CMAlabel performSelectorOnMainThread:@selector(setText:) withObject:labeltext waitUntilDone:NO];
        }];
    }else{
        NSLog(@"改设备不支持获取加速度数据");
    }
    
    
    self.CMGlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 220, 375, 150)];
    self.CMGlabel.numberOfLines = 5;
    self.CMGlabel.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.CMGlabel];
    
    
    //CMMotionManager 支持获取陀螺仪数据
    if (self.motionManager.gyroAvailable) {
        //设置获取陀螺仪数据的更新频率;
        self.motionManager.gyroUpdateInterval = 0.1;
        //使用代码块开始获取陀螺仪数据
        [self.motionManager startGyroUpdatesToQueue:queue withHandler:^(CMGyroData *gyroData, NSError *error) {
            NSString *labeltext;
            if (error) {
                [self.motionManager stopGyroUpdates];
                labeltext  = [NSString stringWithFormat:@"获取陀螺仪数据出现错误%@",error];
                
            }else{
                labeltext = [NSString stringWithFormat:@"绕各轴转速为\n------------------\nX轴%+.2f \nY轴%+.2f \nZ轴%+.2f",
                             gyroData.rotationRate.x,
                             gyroData.rotationRate.y,
                             gyroData.rotationRate.z];
                
                
            }
            [self.CMGlabel performSelectorOnMainThread:@selector(setText:) withObject:labeltext waitUntilDone:NO];
            
            
            
        }];
    }else{
        NSLog(@"该设备不支持获取陀螺仪数据");
    }
    
    
    self.CMMlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 390, 375, 150)];
    [self.view addSubview:self.CMMlabel];
    self.CMMlabel.numberOfLines = 5;
    self.CMMlabel.backgroundColor = [UIColor yellowColor];
    if (self.motionManager.magnetometerAvailable) {
        self.motionManager.magnetometerUpdateInterval = 0.1;
        [self.motionManager startMagnetometerUpdatesToQueue:queue withHandler:^(CMMagnetometerData *magnetometerData, NSError *error) {
            NSString *labeltext;
            if (error) {
                [self.motionManager stopMagnetometerUpdates];
                labeltext = [NSString stringWithFormat:@"获取磁场数据出现错误%@",error];
            }else{
                labeltext = [NSString stringWithFormat:@"磁场数据\n------------------\nX轴%+.2f \nY轴%+.2f \nZ轴%+.2f",
                             magnetometerData.magneticField.x,
                             magnetometerData.magneticField.y,
                             magnetometerData.magneticField.z];
            }
            
            [self.CMMlabel performSelectorOnMainThread:@selector(setText:) withObject:labeltext waitUntilDone:NO];
            
        }];
    }else{
        NSLog(@"该设备不支持获取磁场数据");
    }
}

- (void)typeTwo {
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
